package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallNoticeAdmin;
import org.linlinjava.litemall.db.mapper.LitemallNoticeAdminDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallNoticeAdminService {
    @Resource
    private LitemallNoticeAdminDao noticeAdminMapper;

    public List<LitemallNoticeAdmin> querySelective(String title, String type, Integer adminId, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallNoticeAdmin>  query = noticeAdminMapper.createLambdaQuery();
    	 if(!StringUtils.isEmpty(title)){
    		 query.andLike(LitemallNoticeAdmin::getNoticeTitle, "%" + title + "%");
         }

         if(type.equals("read")){
        	 query.andIsNotNull(LitemallNoticeAdmin::getReadTime);
         }
         else if(type.equals("unread")){
        	 query.andIsNull(LitemallNoticeAdmin::getReadTime);
         }
         query.andEq(LitemallNoticeAdmin::getAdminId, adminId);
         query.andEq(LitemallNoticeAdmin::getDeleted, false);
         
         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }
         query.limit(page, limit);
    	return query.select();
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * LitemallNoticeAdminExample.Criteria criteria = example.createCriteria();
		 * 
		 * if(!StringUtils.isEmpty(title)){ criteria.andNoticeTitleLike("%" + title +
		 * "%"); }
		 * 
		 * if(type.equals("read")){ criteria.andReadTimeIsNotNull(); } else
		 * if(type.equals("unread")){ criteria.andReadTimeIsNull(); }
		 * criteria.andAdminIdEqualTo(adminId); criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * noticeAdminMapper.selectByExample(example);
		 */
    }

    public LitemallNoticeAdmin find(Integer noticeId, Integer adminId) {
    	
    	LitemallNoticeAdmin noticeAdmin = new LitemallNoticeAdmin();
    	noticeAdmin.setNoticeId(noticeId);
    	noticeAdmin.setAdminId(adminId);
    	noticeAdmin.setDeleted(false);
    	return noticeAdminMapper.templateOne(noticeAdmin);
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdEqualTo(noticeId).andAdminIdEqualTo(adminId).
		 * andDeletedEqualTo(false); return
		 * noticeAdminMapper.selectOneByExample(example);
		 */
    }

    public void add(LitemallNoticeAdmin noticeAdmin) {
        noticeAdmin.setAddTime(LocalDateTime.now());
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdminMapper.insertTemplate(noticeAdmin,true);
        //noticeAdminMapper.insertSelective(noticeAdmin);
    }

    public void update(LitemallNoticeAdmin noticeAdmin) {
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdminMapper.updateTemplateById(noticeAdmin);
        //noticeAdminMapper.updateByPrimaryKeySelective(noticeAdmin);
    }

    public void markReadByIds(List<Integer> ids, Integer adminId) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andIn(LitemallNoticeAdmin::getId, ids);
    	query.andEq(LitemallNoticeAdmin::getAdminId, adminId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	LitemallNoticeAdmin update= new LitemallNoticeAdmin();
    	LocalDateTime now = LocalDateTime.now();
    	update.setReadTime(now);
    	update.setUpdateTime(now);
    	query.updateSelective(update);
		/*
		 * 
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andIdIn(ids).andAdminIdEqualTo(adminId).andDeletedEqualTo(false)
		 * ; LitemallNoticeAdmin noticeAdmin = new LitemallNoticeAdmin(); LocalDateTime
		 * now = LocalDateTime.now(); noticeAdmin.setReadTime(now);
		 * noticeAdmin.setUpdateTime(now);
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }

    public void deleteById(Integer id, Integer adminId) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andEq(LitemallNoticeAdmin::getId, id);
    	query.andEq(LitemallNoticeAdmin::getAdminId, adminId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	LitemallNoticeAdmin update= new LitemallNoticeAdmin();
    	LocalDateTime now = LocalDateTime.now();
    	update.setDeleted(true);
    	update.setUpdateTime(now);
    	query.updateSelective(update);
    	
    	
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andIdEqualTo(id).andAdminIdEqualTo(adminId).andDeletedEqualTo(
		 * false); LitemallNoticeAdmin noticeAdmin = new LitemallNoticeAdmin();
		 * noticeAdmin.setUpdateTime(LocalDateTime.now()); noticeAdmin.setDeleted(true);
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }

    public void deleteByIds(List<Integer> ids, Integer adminId) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andIn(LitemallNoticeAdmin::getId, ids);
    	query.andEq(LitemallNoticeAdmin::getAdminId, adminId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	LitemallNoticeAdmin update= new LitemallNoticeAdmin();
    	LocalDateTime now = LocalDateTime.now();
    	update.setDeleted(true);
    	update.setUpdateTime(now);
    	query.updateSelective(update);
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andIdIn(ids).andAdminIdEqualTo(adminId).andDeletedEqualTo(false)
		 * ; LitemallNoticeAdmin noticeAdmin = new LitemallNoticeAdmin();
		 * noticeAdmin.setUpdateTime(LocalDateTime.now()); noticeAdmin.setDeleted(true);
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }

    public int countUnread(Integer adminId) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andIsNull(LitemallNoticeAdmin::getReadTime);
    	query.andEq(LitemallNoticeAdmin::getAdminId, adminId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	return (int)query.count();
		/*
		 * 
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andAdminIdEqualTo(adminId).andReadTimeIsNull().andDeletedEqualTo
		 * (false); return (int)noticeAdminMapper.countByExample(example);
		 */
    }

    public List<LitemallNoticeAdmin> queryByNoticeId(Integer noticeId) {
    	
    	
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andEq(LitemallNoticeAdmin::getNoticeId, noticeId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	return query.select();
    	
    	
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdEqualTo(noticeId).andDeletedEqualTo(false); return
		 * noticeAdminMapper.selectByExample(example);
		 */
    }

    public void deleteByNoticeId(Integer id) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andEq(LitemallNoticeAdmin::getNoticeId, id);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	LitemallNoticeAdmin update= new LitemallNoticeAdmin();
    	LocalDateTime now = LocalDateTime.now();
    	update.setDeleted(true);
    	update.setUpdateTime(now);
    	query.updateSelective(update);
		/*
		 * 
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdEqualTo(id).andDeletedEqualTo(false);
		 * LitemallNoticeAdmin noticeAdmin = new LitemallNoticeAdmin();
		 * noticeAdmin.setUpdateTime(LocalDateTime.now()); noticeAdmin.setDeleted(true);
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }

    public void deleteByNoticeIds(List<Integer> ids) {
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andIn(LitemallNoticeAdmin::getNoticeId, ids);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	LitemallNoticeAdmin update= new LitemallNoticeAdmin();
    	LocalDateTime now = LocalDateTime.now();
    	update.setDeleted(true);
    	update.setUpdateTime(now);
    	query.updateSelective(update);
    	
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdIn(ids).andDeletedEqualTo(false); LitemallNoticeAdmin
		 * noticeAdmin = new LitemallNoticeAdmin();
		 * noticeAdmin.setUpdateTime(LocalDateTime.now()); noticeAdmin.setDeleted(true);
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }

    public int countReadByNoticeId(Integer noticeId) {
    	
    	
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andIsNotNull(LitemallNoticeAdmin::getReadTime);
    	query.andEq(LitemallNoticeAdmin::getNoticeId, noticeId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	return (int)query.count();
		/*
		 * 
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdEqualTo(noticeId).andReadTimeIsNotNull().
		 * andDeletedEqualTo(false); return
		 * (int)noticeAdminMapper.countByExample(example);
		 */
    }

    public void updateByNoticeId(LitemallNoticeAdmin noticeAdmin, Integer noticeId) {
    	LambdaQuery<LitemallNoticeAdmin> query = noticeAdminMapper.createLambdaQuery();
    	query.andEq(LitemallNoticeAdmin::getNoticeId, noticeId);
    	query.andEq(LitemallNoticeAdmin::getDeleted, false);
    	
    	 noticeAdmin.setUpdateTime(LocalDateTime.now());
    	query.updateSelective(noticeAdmin);
    	
		/*
		 * LitemallNoticeAdminExample example = new LitemallNoticeAdminExample();
		 * example.or().andNoticeIdEqualTo(noticeId).andDeletedEqualTo(false);
		 * noticeAdmin.setUpdateTime(LocalDateTime.now());
		 * noticeAdminMapper.updateByExampleSelective(noticeAdmin, example);
		 */
    }
}
